package cn.rongdao.jrkabao.ui.information;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.ArrayList;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;


/**
 * Created by jiangyongxing on 2016/10/18.
 * 描述：紧急联系人界面
 */

public class EmergencyContactActivity extends BaseAutoFinishActivity implements View.OnClickListener {

    private EditText mNameEt;
    private EditText mNumberEt;
    private ArrayList<String> infoList;
    private TextView mRelationshipTv;
    private ActionSheetDialog dialog;
    private int position;
    private String relationship;
    private int relationshipPos;
    private String nameFrom;
    private String numberFrom;
    private int valueFrom = -1;
    private TitleBarUIComponent titleBarUIComponent;
    @Override
    protected int layoutRes() {
        return R.layout.activity_emergency_contact;
    }
    @Override
    protected void initView() {
        initData();
        titleBarUIComponent = (TitleBarUIComponent) findViewById(R.id.emergency_contact_titlebar);
        initTitleBar();
        RelativeLayout mNumberRl = (RelativeLayout) findViewById(R.id.activity_emergency_contact_number_rl);
        mNumberRl.setOnClickListener(this);
        mNameEt = (EditText) findViewById(R.id.activity_emergency_contact_name_et);
        mNumberEt = (EditText) findViewById(R.id.activity_emergency_contact_number_et);
        RelativeLayout mRelationshipRl = (RelativeLayout) findViewById(R.id.activity_emergency_contact_relationship_rl);
        mRelationshipRl.setOnClickListener(this);
        mRelationshipTv = (TextView) findViewById(R.id.activity_emergency_contact_relationship_tv);
        Button mFinishButton = (Button) findViewById(R.id.activity_emergency_contact_finish_button);
        mFinishButton.setOnClickListener(this);
        if (valueFrom != -1) {
            mRelationshipTv.setText(infoList.get(valueFrom));
        }
        if (!TextUtils.isEmpty(nameFrom)) {
            mNameEt.setText(nameFrom);
        }
        if (!TextUtils.isEmpty(numberFrom)) {
            mNumberEt.setText(numberFrom);
        }


    }

    private void initTitleBar() {
        titleBarUIComponent.initTitleBlack("紧急联系人");
        titleBarUIComponent.setBottomViewVisiable(true);
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        titleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void initData() {
        try {
            infoList = getIntent().getStringArrayListExtra("data");
            String positionStr = getIntent().getStringExtra("position");
            position = Integer.parseInt(positionStr);
            String valueStr = getIntent().getStringExtra("value");
            nameFrom = getIntent().getStringExtra("name");
            numberFrom = getIntent().getStringExtra("number");
            valueFrom = Integer.parseInt(valueStr);
            relationshipPos = valueFrom;
            String title = getIntent().getStringExtra("title");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_emergency_contact_number_rl:

                break;
            case R.id.activity_emergency_contact_relationship_rl:
                if (infoList != null && infoList.size() > 0) {
                    ActionSheetDialog();
                }
                break;
            case R.id.activity_emergency_contact_finish_button:
                String name = mNameEt.getText().toString().trim();
                String number = mNumberEt.getText().toString().trim();
                relationship = mRelationshipTv.getText().toString().trim();
                if (TextUtils.isEmpty(relationship)) {
                    FRToast.showToastSafe("请选择与本人关系");
                    return;
                }
                if (TextUtils.isEmpty(name)) {
                    FRToast.showToastSafe("请填写联系人姓名");
                    return;
                }
                if (TextUtils.isEmpty(number)) {
                    FRToast.showToastSafe("请填写联系人手机号码");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("position", position);
                intent.putExtra("relationship", relationship);
                intent.putExtra("number", number);
                intent.putExtra("name", name);
                intent.putExtra("relationshipPos", relationshipPos);
                this.setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }


    private void ActionSheetDialog() {
        String[] stringItems = new String[infoList.size()];
        for (int i = 0; i < stringItems.length; i++) {
            stringItems[i] = infoList.get(i);
        }

        dialog = new ActionSheetDialog(this, stringItems, null);
        dialog.title("选择紧急联系人")//
                .titleTextSize_SP(14.5f)//
                .show();

        dialog.setOnOperItemClickL(new OnOperItemClickL() {

            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int pos, long id) {
                relationship = infoList.get(pos);
                relationshipPos = pos;
                mRelationshipTv.setText(relationship);
                dialog.dismiss();
            }
        });
    }



}
